home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / faxstat / FaxStatClient.c++ < prev    next >
C/C++ Source or Header  |  1994-08-01  |  5KB  |  191 lines

  1. /*    $Header: /usr/people/sam/fax/faxstat/RCS/FaxStatClient.c++,v 1.12 1994/03/09 18:47:57 sam Rel $ */
  2. /*
  3.  * Copyright (c) 1990, 1991, 1992, 1993, 1994 Sam Leffler
  4.  * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and 
  7.  * its documentation for any purpose is hereby granted without fee, provided
  8.  * that (i) the above copyright notices and this permission notice appear in
  9.  * all copies of the software and related documentation, and (ii) the names of
  10.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11.  * publicity relating to the software without the specific, prior written
  12.  * permission of Sam Leffler and Silicon Graphics.
  13.  * 
  14.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  15.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  16.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  17.  * 
  18.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  22.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  23.  * OF THIS SOFTWARE.
  24.  */
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28.  
  29. #include "FaxStatClient.h"
  30. #include "SendStatus.h"
  31. #include "RecvStatus.h"
  32. #include "ServerStatus.h"
  33.  
  34. // FaxStatClient public methods
  35.  
  36. int FaxStatClient::clientsDone = 0;
  37.  
  38. FaxStatClient::FaxStatClient(const char* host,
  39.     FaxSendStatusArray& sa, FaxRecvStatusArray& ra, FaxServerStatusArray& va)
  40.     : FaxClient(host)
  41.     , sendStatus(sa)
  42.     , recvStatus(ra)
  43.     , serverStatus(va)
  44. {
  45.     iamDone = FALSE;
  46. }
  47.  
  48. FaxStatClient::~FaxStatClient()
  49. {
  50.     if (iamDone)
  51.     clientsDone--;        // reset so that repeated use works correctly
  52. }
  53.  
  54. fxBool
  55. FaxStatClient::start(fxBool debug)
  56. {
  57.     if (debug)
  58.     setFds(0, 1);        // use stdin, stdout instead of socket
  59.     else if (!callServer()) {
  60.     printError("Could not call server on host %s", (char*) getHost());
  61.     return (FALSE);
  62.     }
  63.     return (TRUE);
  64. }
  65.  
  66. void
  67. FaxStatClient::printError(const char* va_alist ...)
  68. #define    fmt va_alist
  69. {
  70.     va_list ap;
  71.     va_start(ap, va_alist);
  72.     vfprintf(stderr, fmt, ap);
  73.     va_end(ap);
  74.     fputs("\n", stderr);
  75. }
  76. #undef fmt
  77.  
  78. void
  79. FaxStatClient::printWarning(const char* va_alist ...)
  80. #define    fmt va_alist
  81. {
  82.     va_list ap;
  83.     va_start(ap, va_alist);
  84.     fprintf(stderr, "Warning, ");
  85.     vfprintf(stderr, fmt, ap);
  86.     va_end(ap);
  87.     fputs("\n", stderr);
  88. }
  89. #undef fmt
  90.  
  91. #define    isCmd(s)    (strcasecmp(s, cmd) == 0)
  92.  
  93. void
  94. FaxStatClient::recvConf(const char* cmd, const char* tag)
  95. {
  96.     if (isCmd("server")) {
  97.     FaxServerStatus stat;
  98.     if (stat.parse(tag)) {
  99.         stat.host = getHost();
  100.         serverStatus.append(stat);
  101.     }
  102.     } else if (isCmd("jobStatus")) {
  103.     if (!parseSendStatus(tag)) {
  104.         printError("Malformed statusMessage \"%s\"\n", tag);
  105.         goto bad;
  106.     }
  107.     } else if (isCmd("jobAtStatus")) {
  108.     if (!parseSendAtStatus(tag)) {
  109.         printError("Malformed statusMessage \"%s\"\n", tag);
  110.         goto bad;
  111.     }
  112.     } else if (isCmd("recvJob")) {
  113.     if (!parseRecvStatus(tag)) {
  114.         printError("Malformed statusMessage \"%s\"\n", tag);
  115.         goto bad;
  116.     }
  117.     } else if (isCmd("error")) {
  118.     printf("%s\n", tag);
  119.     } else if (isCmd(".")) {
  120. bad:
  121.     handleEof(0, FALSE);
  122.     } else {
  123.     printError("Unknown status message \"%s:%s\"\n", cmd, tag);
  124.     }
  125. }
  126.  
  127. fxBool
  128. FaxStatClient::parseSendStatus(const char* tag)
  129. {
  130.     FaxSendStatus stat;
  131.  
  132.     if (stat.parse(tag)) {
  133.     stat.host = getHost();
  134.     sendStatus.append(stat);
  135.     return (TRUE);
  136.     } else
  137.     return (FALSE);
  138. }
  139.  
  140. fxBool
  141. FaxStatClient::parseSendAtStatus(const char* tag)
  142. {
  143.     FaxSendAtStatus stat;
  144.  
  145.     if (stat.parse(tag)) {
  146.     stat.host = getHost();
  147.     sendStatus.append(stat);
  148.     return (TRUE);
  149.     } else
  150.     return (FALSE);
  151. }
  152.  
  153. fxBool
  154. FaxStatClient::parseRecvStatus(const char* tag)
  155. {
  156.     FaxRecvStatus stat;
  157.  
  158.     if (stat.parse(tag)) {
  159.     stat.host = getHost();
  160.     recvStatus.append(stat);
  161.     return (TRUE);
  162.     } else
  163.     return (FALSE);
  164. }
  165.  
  166. void
  167. FaxStatClient::recvEof()
  168. {
  169.     handleEof(0, FALSE);
  170. }
  171.  
  172. void
  173. FaxStatClient::recvError(const int err)
  174. {
  175.     handleEof(err, TRUE);
  176. }
  177.  
  178. // FaxStatClient private methods
  179.  
  180. void
  181. FaxStatClient::handleEof(const int err, const fxBool isError)
  182. {
  183.     if (isError)
  184.     printError("Socket read error: %s\n", strerror(err));
  185.     (void) hangupServer();
  186.     clientsDone++;
  187.     iamDone = TRUE;
  188. }
  189.  
  190. fxIMPLEMENT_PtrArray(FaxStatClientArray, FaxStatClient*);
  191.